package cn.kawa.org.service.impl;

import cn.kawa.basic.query.BaseQuery;
import cn.kawa.basic.service.impl.BaseServiceImpl;
import cn.kawa.basic.util.PageList;
import cn.kawa.org.domain.Department;
import cn.kawa.org.mapper.DepartmentMapper;
import cn.kawa.org.query.DepartmentQuery;
import cn.kawa.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /*@Override
    public List<Department> findAll() {
        return departmentMapper.loadAll();
    }*/

    /*@Override
    public Department findById(Long id) {
        return departmentMapper.loadById(id);
    }*/
    @Transactional
    @Override
    public void add(Department department) {
        departmentMapper.save(department);

        update(department);
    }

    @Transactional
    @Override
    public void update(Department department) {
        if(department.getParent_id()==null){
            Long id = department.getId();
            department.setDirPath("/"+id);
        }else{//2.有上级部门
            Long parent_id = department.getParent_id();
            Department departmentParent = departmentMapper.loadById(parent_id);
            department.setDirPath(departmentParent.getDirPath()+"/"+department.getId());

        }
        departmentMapper.update(department);
    }


    /*@Transactional
    @Override
    public void delete(Long id) {
        departmentMapper.remove(id);
    }*/

    @Override//分页查询数据:   注意此处的BaseQuery是多态写法，内容为DepartmentQuery
    public PageList<Department> queryPage(BaseQuery query) {
        //查询总数量
        Integer count = departmentMapper.queryCount(query);
        //如果为空就返回空集合
        if(count ==null|| count==0){
            return new PageList<>();
        }
        //不为空就查询list，并和count放入PageList对象里返回
        List<Department> departments = departmentMapper.queryData(query);
        return new PageList<>(count,departments);
    }

    /*@Override
    @Transactional
    public void patchDel(List<Long> ids) {
        departmentMapper.patchDel(ids);
    }*/

    @Override
    public List<Department> deptTree() {
        List<Department> departments = departmentMapper.loadAll();
        Map<Long,Department> map = new HashMap<>();
        for (Department department : departments) {
            map.put(department.getId(),department);
        }
        List<Department> deptTree = new ArrayList<>();
        for (Department department : departments) {
            if(department.getParent_id()==null){
                deptTree.add(department);
            }else {
                //通过当前部门获取上级id
                Long parent_id =department.getParent_id();
                Department parentDepartment = map.get(parent_id);
                parentDepartment.getChildren().add(department);
            }
        }

        return deptTree;
    }
}
